package com.jxb.dialog.utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;

/**
 * @author jinxb
 * date     : 2021/7/13
 * desc     :
 */
public class ProgressDialogUtils {
    private static ProgressDialog mProgressDialog;
    private static boolean isLiving(Activity activity) {

        if (activity == null) {
            Log.d("wisely", "activity == null");
            return false;
        }

        if (activity.isFinishing()) {
            Log.d("wisely", "activity is finishing");
            return false;
        }
        return true;
    }
    public static void showProgressDialog(Activity context) {
        showProgressDialog(context, "", "",true);
    }
    public static void showProgressDialogUnCancelable(Activity context) {
        showProgressDialog(context, "", "",false);
    }
    public static void showProgressDialog(Activity context,String title, String msg) {
        showProgressDialog(context, title, msg,true);
    }
    public static void showProgressDialogUnCancelable(Activity context,String title, String msg) {
        showProgressDialog(context, title, msg,false);
    }
    /**
     * 进度dialog
     */
    private static void showProgressDialog(Activity activity, String title, String msg, boolean cancelable) {
        if (!isLiving(activity)) {
            return;
        }
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(activity);
        }
        //依次设置标题,内容,是否用取消按钮关闭,是否显示进度
        mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(msg);
        mProgressDialog.setCancelable(cancelable);
        //这里是设置进度条的风格,HORIZONTAL是水平进度条,SPINNER是圆形进度条
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setIndeterminate(true);
        //调用show()方法将ProgressDialog显示出来
        mProgressDialog.show();
    }



    public static void dismissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
        mProgressDialog = null;
    }
}
